package com.foreknow.elm.dao.Impl;

import com.foreknow.elm.dao.OrderDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private Connection conn;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public int OrdersInsert(String userId, Integer businessId, Integer daId, Double orderTotal) throws SQLException {
        int OrderId=0;
        String sql="INSERT INTO orders (userId,businessId,orderTotal,daId,orderDate) VALUES(?,?,?,?,?)";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String date1=dateFormat.format(date);
        try {
            conn=DBUtil.getConnection();
            pst=conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setDouble(3,orderTotal);
            pst.setInt(4,daId);
            pst.setString(5,date1);
            pst.executeUpdate();
            rs= pst.getGeneratedKeys();
            if (rs.next()){
                OrderId=rs.getInt(1);
            }
        return OrderId;
    }finally {
            DBUtil.close(pst);
        }
}

    @Override
    public List<Cart> OrdersSelect(String userId, Integer businessId) throws SQLException {
        List<Cart> list=new ArrayList<>();
        try {
            conn=DBUtil.getConnection();
            String sqlGetCart="SELECT foodId,quantity from cart WHERE userId=? and businessId=?";
            pst=conn.prepareStatement(sqlGetCart);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            rs=pst.executeQuery();
            while (rs.next()){
                Cart cart=new Cart();
                cart.setFoodId(rs.getInt("FoodId"));
                cart.setQuantity(rs.getInt("Quantity"));
                list.add(cart);
            }
        }finally {
            DBUtil.close(pst);
        }
        return list;
    }


    @Override
    public int CartRemove(String userId,Integer businessId)throws SQLException {
        int a=0;
        String sqlDelete="DELETE FROM cart WHERE userId=? and businessId=?";
        try {
            conn=DBUtil.getConnection();
            pst=conn.prepareStatement(sqlDelete);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            a=pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return a;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws SQLException {
//        OrderdetailetDao odd=new OrderdetailetDaoImpl();
        Orders orders=new Orders();
//        List<Orderdetailet> orderdetailets=odd.getOD(orderId);
//        orders.setList(orderdetailets);
        String sql="SELECT DISTINCT o.*,b.* FROM orders o JOIN business b on o.businessId=b.businessId and o.orderId=?";
        try {
            conn=DBUtil.getConnection();
            pst= conn.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs= pst.executeQuery();
            while (rs.next()){
                Business bu=new Business();
                bu.setBusinessId(rs.getInt("BusinessId"));
                bu.setBusinessName(rs.getString("BusinessName"));
                bu.setBusinessAddress(rs.getString("BusinessAddress"));
                bu.setBusinessExplain(rs.getString("BusinessExplain"));
                bu.setBusinessImg(rs.getString("BusinessImg"));
                bu.setOrderTypeId(rs.getInt("OrderTypeId"));
                bu.setStarPrice(rs.getDouble("StarPrice"));
                bu.setDeliveryPrice(rs.getDouble("DeliveryPrice"));
                bu.setRemarks(rs.getString("Remarks"));
                orders.setBusiness(bu);
                orders.setOrderId(rs.getInt("OrderId"));
                orders.setUserId(rs.getString("UserId"));
                orders.setBusinessId(rs.getInt("BusinessId"));
                orders.setOrderDate(rs.getString("OrderDate"));
                orders.setOrderTotal(rs.getDouble("OrderTotal"));
                orders.setDaId(rs.getInt("DaId"));
                orders.setOrderState(rs.getInt("OrderState"));
            }
        }finally {
                DBUtil.close(pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws SQLException {
        List<Orders>li=new ArrayList<>();
//        OrderdetailetDao odd=new OrderdetailetDaoImpl();
        String sql="SELECT o.*,b.* FROM orders o JOIN business b on o.businessId=b.businessId and o.userId=?";
        try {
            conn=DBUtil.getConnection();
            pst= conn.prepareStatement(sql);
            pst.setString(1,userId);
            rs= pst.executeQuery();
            while (rs.next()) {
                Business bu = new Business();
                bu.setBusinessId(rs.getInt("BusinessId"));
                bu.setBusinessName(rs.getString("BusinessName"));
                bu.setBusinessAddress(rs.getString("BusinessAddress"));
                bu.setBusinessExplain(rs.getString("BusinessExplain"));
                bu.setBusinessImg(rs.getString("BusinessImg"));
                bu.setOrderTypeId(rs.getInt("OrderTypeId"));
                bu.setStarPrice(rs.getDouble("StarPrice"));
                bu.setDeliveryPrice(rs.getDouble("DeliveryPrice"));
                bu.setRemarks(rs.getString("Remarks"));
                Orders orders = new Orders();
                orders.setBusiness(bu);
                orders.setOrderId(rs.getInt("OrderId"));
                orders.setUserId(rs.getString("UserId"));
                orders.setBusinessId(rs.getInt("BusinessId"));
                orders.setOrderDate(rs.getString("OrderDate"));
                orders.setOrderTotal(rs.getDouble("OrderTotal"));
                orders.setDaId(rs.getInt("DaId"));
                orders.setOrderState(rs.getInt("OrderState"));
//                orders.setList(odd.getOD(orders.getOrderId()));
                li.add(orders);
            }
        }finally {
            DBUtil.close(pst);
        }
        return li;
    }

    public static void main(String[] args) throws SQLException {
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String date1=dateFormat.format(date);
        System.out.println(date1);
        OrderDao od=new OrderDaoImpl();

//        List<Orders>list=od.listOrdersByUserId("11111111111");
//        for (int i=0;i< list.size();i++){
//            System.out.println(list.get(i).getOrderId()+"---"+list.get(i).getList().size());
//        }

        Orders o=od.getOrdersById(6);
        System.out.println(o.getBusinessId()+"---"+o.getOrderId()+"---"+o.getBusiness().getBusinessId()+"---"+o.getBusiness().getBusinessName());


//        Orderdetailet o1=new Orderdetailet();
//        o1.setOrderId(103);
//        o1.setFoodId(1);
//        o1.setQuantity(14);
//        Orderdetailet o2=new Orderdetailet();
//        o2.setOrderId(104);
//        o2.setFoodId(5);
//        o2.setQuantity(15);
//        List<Orderdetailet> list=new ArrayList<>();
//        list.add(o1);
//        list.add(o2);
//        System.out.println(od.OrdersDeInsert(list));

//        StringBuffer s=new StringBuffer("INSERT INTO orderdetailet (orderId,foodId,quantity) VALUES(?,?,?) ");
//        s.delete(s.lastIndexOf("("), s.lastIndexOf(" "));
//        System.out.println(s);

//        System.out.println(od.CartRemove("12345",12345));
//        Orders os=od.getOrdersById(1);
//        List<Orders>list=od.listOrdersByUserId("12345671111");
//        System.out.println(list.size());
//        System.out.println(os.getUserId());
//        for (int i=0;i<os.getList().size();i++){
//            System.out.println(os.getList().get(i).getFoodId()+"---"+os.getList().get(i).getQuantity());
//        }
//        List<Cart>list= od.OrdersSelect("12345671111",10001);
//        System.out.println(od.OrdersDeInsert(14,list));
//        for (int i=0;i< list.size();i++) {
//            System.out.println(list.get(i).getFoodId() + "---" + list.get(i).getQuantity());
//        }
//        int i=od.OrdersInsert("12345671111",10004,2,60.5);
//        System.out.println(i);
//        int i= od.createOrders("4444444444",10001,5,150.5);
//        System.out.println(i);
    }
}
